var express = require('express');
var router = express.Router();
var identityKey = 'skey';

const Util = require('../note/Util');
const db = require('../note/db');


// 定义当前路由主页
router.get('/', function(req, res) {
   res.render('note', { title: 'note', data:{msg:'hello node!',content:'我只想说一句MMP',uname:'suoge'} });
});
router.get('/[0-9]?*', function(req, res) {
   console.log('query',req.query);
   var arr = req.url.split('/');
   // 获取路径上的参数
   for (var i = 0, length = arr.length; i < length; i++) {
     arr[i] = arr[i].split('?')[0];
   }
   console.log('参数长度'+arr.length+'内容为：'+JSON.stringify(arr));
   var noteId = arr[1];
   var uid = req.session.loginUserId;
   if(!uid){
     return res.redirect('/logout');
   }
   try {
     db.notes.find({
       where:{
         uid:uid,
         id:noteId,
         status:0,
       }
     }).then(ret=>{
         console.log('信息',ret);
         var note_info = ret;
         var nid = note_info['id'];
         try {
           db.contents.find({
             where:{
               nid:nid
             }
           }).then(ret=>{
             console.log('内容',ret);
             var contents = ret;
             note_info.contents = contents;
             res.render('note', { title: note_info.title, data:{msg:note_info.title,code:520,content:'MMP',datas:note_info},util:Util});
           })

         } catch (e) {
           res.render('error', { title: '笔记不存在', data:{msg:'笔记不存在!',code:110} });
          console.log('笔记未找到',e);
         }
     }).catch(e=>{
        res.render('error', { title: '笔记不存在', data:{msg:'笔记不存在!',code:110} });
       console.log('笔记未找到',e);
     })
   } catch (e) {
     console.log(e);
   } finally {

   }

});



module.exports = router;
